package app.gt.screen.routerequest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import app.gt.R;
import app.gt.screen.routerequest.map.SearchOnMap;

public class DefineRouteScreen extends Activity implements
		android.view.View.OnClickListener {
	LinearLayout MainAddStop;
	LinearLayout HelpMap;
	LinearLayout EdittextString;
	EditText addstop;
	int toggle = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.defineroutescreen);

		initScreen();
	}

	private void initScreen() {
		// TODO Auto-generated method stub
		initTemporaryStopLayout();
		initUseMap();
		initView();
	}

	private void initView() {
		// TODO Auto-generated method stub
		Button SearchRoute = (Button)findViewById(R.id.DefineRouteScreen_Search_Route);
		SearchRoute.setOnClickListener(this);
	}

	private void initUseMap() {
		// TODO Auto-generated method stub
		HelpMap = (LinearLayout) findViewById(R.id.UseMapHelp);
		HelpMap.setOnClickListener(this);
	}

	private void initTemporaryStopLayout() {
		// TODO Auto-generated method stub
		initEditTextString();
		//initAddTemporaryStopToLayout();
		
		
	}

	private void initAddTemporaryStopToLayout(int set) {
		// TODO Auto-generated method stub
		//find parent layout
		LinearLayout whu = (LinearLayout) findViewById(R.id.WhereAreYouLayout);
		//define new temporary layout
		LinearLayout  TemporaryStop = new LinearLayout(this);
		TemporaryStop.setLayoutParams(new LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		TemporaryStop.setGravity(Gravity.HORIZONTAL_GRAVITY_MASK);
		ImageView imageT = new ImageView(this);
		imageT.setImageResource(R.drawable.icon);
		imageT.setLayoutParams(new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 6));
		EditText EditTStop = new EditText(this);
		EditTStop.setLayoutParams(new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1));
		EditTStop.setText(addstop.getText().toString());
		TemporaryStop.addView(imageT);
		TemporaryStop.addView(EditTStop);
		
		int number = set;
		
		TextView TempStopText= new TextView(this);
		TempStopText.setText("Temporary stop"+" "+number);
		
		whu.addView(TempStopText);
		whu.addView(TemporaryStop);
		
	}

	private void initEditTextString() {
		// TODO Auto-generated method stub
		MainAddStop = (LinearLayout) findViewById(R.id.AddtempStopMainLayout);
		LinearLayout AddStop = (LinearLayout) findViewById(R.id.AddtempStopLayout);
		AddStop.setOnClickListener(this);
		EdittextString = new LinearLayout(this);
		EdittextString.setLayoutParams(new LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		EdittextString.setGravity(Gravity.HORIZONTAL_GRAVITY_MASK);
		ImageView i2 = new ImageView(this);
		i2.setId(1);
		i2.setImageResource(R.drawable.forward);
		i2.setLayoutParams(new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 6));
		i2.setOnClickListener(this);
		addstop = new EditText(this);
		addstop.setLayoutParams(new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1));
		// EdittextString.addView(i1);
		EdittextString.addView(addstop);
		EdittextString.addView(i2);
	}
int i = 0;
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.AddtempStopLayout:

			if (toggle == 0) {
				MainAddStop.addView(EdittextString);
				toggle = 1;
			} else {
				MainAddStop.removeView(EdittextString);
				toggle = 0;
			}
			break;

		case R.id.UseMapHelp:

			Intent maphelp = new Intent(this, SearchOnMap.class);
			startActivity(maphelp);
			break;
		case 1:
			i=i+1;
			initAddTemporaryStopToLayout(i);
			
			break;

		}// TODO Auto-generated method stub

	}

}
